package org.dtrd.modules.faq.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.common.aspect.annotation.AutoLog;
import org.dtrd.modules.faq.entity.dto.FaqCategoryDetail;
import org.dtrd.modules.faq.service.IDtrdExtEmsYwFaqCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static org.dtrd.common.constant.CommonConstant.*;
import static org.dtrd.config.system.SystemState.ResponseState.*;

/**
 * <p>
 * 知识问答分类 前端控制器
 * </p>
 * @author QR
 * @since 2021-12-06
 */
@Api(tags = "常见问题")
@RestController
@RequestMapping("/100/15/")
public class FaqCategoryController extends BaseController {

    @Autowired
    private IDtrdExtEmsYwFaqCategoryService faqCategoryService;


    @ApiOperation(value = "获取常见问题分类详情", response = FaqCategoryDetail.class)
    @GetMapping("1001501")
    @AutoLog(value="获取常见问题分类详情", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getFaqCategory(@RequestParam Integer id){
        if (isIntParamInvalid(id)) {
            return error(STATE_PARAMS_ERROR);
        }
        FaqCategoryDetail faqCategoryInfo = faqCategoryService.getFaqCategory(id);
        return Result.OK(faqCategoryInfo);
    }

    @ApiOperation(value = "获取常见问题分类列表", response = FaqCategoryDetail.class)
    @GetMapping("1001502")
    @AutoLog(value="获取常见问题分类列表", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getFaqCategoryList(@ApiParam(value = "饮食分类", example = "饮食") @RequestParam(required = false) String faqCategoryName){
        List<FaqCategoryDetail> faqCategoryList = faqCategoryService.getFaqCategoryList(faqCategoryName);
        return Result.OK(faqCategoryList);
    }

    @ApiOperation("更新或修改常见问题分类")
    @PostMapping("1001503")
    @AutoLog(value="更新或修改常见问题分类", operateType = OPERATE_TYPE_SAVE_OR_UPDATE)
    public Result<?> saveOrUpdateFaqCategory(@RequestBody FaqCategoryDetail dto){
        boolean result = faqCategoryService.saveOrUpdateFaqCategory(dto);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }

    @ApiOperation("删除常见问题分类")
    @DeleteMapping("1001504")
    @AutoLog(value="删除常见问题分类", operateType = OPERATE_TYPE_DELETE)
    public Result<?> removeFaqCategory(@RequestParam Integer id){
        if (isIntParamInvalid(id)) {
            return error(STATE_PARAMS_MISSING);
        }
        boolean result = faqCategoryService.removeFaqCategory(id);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }
}
